package net.ezsteam;

import java.util.ArrayList;
import java.util.HashMap;

import net.ezsteam.cons.EzSteam_Urls;
import net.ezsteam.object.EzSteam_ListItem;
import net.ezsteam.object.EzSteam_Vehicle;
import net.ezsteam.service.EzSteam_Add_OneCar;
import net.ezsteam.service.EzSteam_Login_Service_Rest;
import net.ezsteam.service.EzSteam_Register_Service;
import net.ezsteam.service.EzSteam_Service_ServiceType;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class EzSteam_Register_Activity extends Activity{

	private ListView myList;
    private MyAdapter myAdapter;
    public ArrayList<EzSteam_ListItem> myItems = new ArrayList<EzSteam_ListItem>();
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ezsteam_register);

        myList = (ListView) findViewById(R.id.list_vehicles);

        LayoutInflater inflater = getLayoutInflater();
        ViewGroup header = (ViewGroup)inflater.inflate(R.layout.ezsteam_register_header, myList, false);
        ViewGroup footer = (ViewGroup)inflater.inflate(R.layout.ezsteam_register_footer, myList, false);
        myList.addHeaderView(header, null, false);
        myList.addFooterView(footer, null, false);
        
        Button btn_submit = (Button)findViewById(R.id.btn_submit);
        final EditText txt_name = (EditText)findViewById(R.id.txt_name);
        final EditText txt_email = (EditText)findViewById(R.id.txt_email);
        final EditText txt_password = (EditText)findViewById(R.id.txt_password);
        
        final Spinner type_spnr = (Spinner)findViewById(R.id.spn_servicetype);
        
        EzSteam_Service_ServiceType sstype = new EzSteam_Service_ServiceType(EzSteam_Register_Activity.this,type_spnr);
        sstype.execute(EzSteam_Urls.GET_Service_Type);
        
        btn_submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				dropEmptyCars();
				
				if(myItems.size()!=0){
					/*EzSteam_Register_Service rq = new EzSteam_Register_Service(EzSteam_Register_Activity.this,txt_name.getText().toString(),txt_email.getText().toString(),txt_password.getText().toString(),type_spnr.getSelectedItem().toString(),myItems);
					rq.execute(EzSteam_Urls.Register_Service);*/
					EzSteam_Register_Service rq = new EzSteam_Register_Service(EzSteam_Register_Activity.this,txt_name.getText().toString(),txt_email.getText().toString(),txt_password.getText().toString(),""+type_spnr.getSelectedItemPosition(),myItems);
					rq.execute(EzSteam_Urls.Register_Service);
				}else{
					Toast toast = Toast.makeText(EzSteam_Register_Activity.this, "No Valid Cars Added !!", Toast.LENGTH_SHORT);
					toast.show();
				}
			}
		});
        
       /* myList.setOnTouchListener(new ListView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                case MotionEvent.ACTION_DOWN:
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    break;

                case MotionEvent.ACTION_UP:
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }

                v.onTouchEvent(event);
                return true;
            }
        });*/
        

        myList.setItemsCanFocus(true);
        myAdapter = new MyAdapter();

        myList.setAdapter(myAdapter);
        
        myAdapter.notifyDataSetChanged();
        //myAdapter.notifyDataSetInvalidated();
        
        Button addmore = (Button)findViewById(R.id.btn_Add_More);
        addmore.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final Dialog dialog = new Dialog(EzSteam_Register_Activity.this);

				dialog.setContentView(R.layout.ezsteam_vehicle_row);
				dialog.setTitle("Add New Car");

				final EditText txtMake = (EditText)dialog.findViewById(R.id.txt_make);
				final EditText txtModel = (EditText)dialog.findViewById(R.id.txt_model);
				final EditText txtYear = (EditText)dialog.findViewById(R.id.txt_year);
				
				Button btndone = (Button)dialog.findViewById(R.id.btn_remove);
				
				btndone.setText("Add car");
				
				btndone.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						
						if(!txtMake.getText().toString().trim().equals("")||!txtModel.getText().toString().trim().equals("")||!txtYear.getText().toString().trim().equals("")){

							EzSteam_ListItem listItem = new EzSteam_ListItem(myAdapter.getCount(),txtMake.getText().toString().trim(),txtModel.getText().toString().trim(),txtYear.getText().toString().trim());
					        
							Log.i("myAdapter.getCount()",""+myAdapter.getCount());
							
							myItems.add(listItem);
					        myAdapter.notifyDataSetChanged();
					        
					        dialog.dismiss();
							
						}else{
							Toast toast = Toast.makeText(EzSteam_Register_Activity.this, "No Valid Data !!", Toast.LENGTH_SHORT);
							toast.show();
						}
						
						
					}
				});
				
				dialog.show();
			}
		});
        
	}
	
	public class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public MyAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           
        }

        public int getCount() {
            return myItems.size();
        }

        public String getItem(int position) {
            return ""+position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            final int pos = position;

            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.ezsteam_vehicle_row, null);
                holder.text = (TextView)convertView.findViewById(R.id.txt_incr);
                holder.make=(EditText)convertView.findViewById(R.id.txt_make); 
                holder.model = (EditText) convertView.findViewById(R.id.txt_model);
                holder.year = (EditText) convertView.findViewById(R.id.txt_year);

                holder.deleteButton = (Button) convertView.findViewById(R.id.btn_remove);

               

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.make.setText(myItems.get(position).getEditMake());
            holder.model.setText(myItems.get(position).getEditModel());
            holder.year.setText(myItems.get(position).getEditYear());
            
            holder.make.setTag(position);
            holder.model.setTag(position);
            holder.year.setTag(position);

            holder.deleteButton.setTag(position);

            holder.deleteButton.setFocusable(false);

            holder.deleteButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    int tag = (Integer) view.getTag();
                    if (pos != 0) {
                        myItems.remove(pos);
                        Log.d("GCM", "Item removed from " + pos);
                        myAdapter.notifyDataSetChanged();
                    } else {

                    }
                }
            });
            
            return convertView;
        }

    }

    class ViewHolder {

    	TextView text;
    	EditText make;
        EditText model;
        EditText year;
        Button deleteButton;
    }

    private void dropEmptyCars(){
		for(int loop=0;loop<myItems.size();loop++){
			if(myItems.get(loop).getEditMake().equals("") || myItems.get(loop).getEditModel().equals("") || myItems.get(loop).getEditYear().equals("")){
				myItems.remove(loop);
				 myAdapter.notifyDataSetChanged();
			}
		}
	}
   
}
